import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeSupport;
import java.io.Console;
import java.io.File;
import java.io.FileFilter;
/**
 * Heads Up Display, Shows the current item levels, position on the map
 * total distance traveled, etc.
 * @author Hobosapiens
 */
public class HUD extends JPanel
{
    Ship ship;
    Timeline timeline;
    SaveLoad saver;
    JLabel waterAmtLbl, foodAmtLbl, repairAmtLbl, medicineAmtLbl, moneyLbl,
            weightLbl, totalDistance, distToNext, stopName;
    
    JComboBox paceCB, rationsCB;
    JButton save, load;
    JPanel map;
    Map mapClass;
    PropertyChangeSupport status;
    
    /**
     * Constructor
     * @param  s ship
     * @param  t timeline
     */
    public HUD(Ship s, Timeline t)
    {
        ship = s;
        timeline = t;
        saver = new SaveLoad(ship,timeline);

        waterAmtLbl = new JLabel("Current water: ");
        foodAmtLbl = new JLabel("Current food: ");
        repairAmtLbl = new JLabel("Current repair kits: ");
        medicineAmtLbl = new JLabel("Current medicine: ");
        moneyLbl = new JLabel("Current money: ");
        weightLbl = new JLabel("Current weight: ");
        
        totalDistance = new JLabel("Total Distance: ");
        distToNext = new JLabel("Distance to next stop: ");
        stopName = new JLabel("Current stop: ");
        
        mapClass = new Map();
        map = mapClass.getMap();
        
        status = new PropertyChangeSupport(this);
        
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        setMaximumSize(new Dimension(800, 150));
        
        JPanel itemLbls = new JPanel();
        itemLbls.setAlignmentX(Component.LEFT_ALIGNMENT);
        itemLbls.setLayout(new GridLayout(4, 3));//new BoxLayout(itemLbls, BoxLayout.PAGE_AXIS));

        itemLbls.add(foodAmtLbl);
        itemLbls.add(moneyLbl);
        itemLbls.add(stopName);
        
        itemLbls.add(waterAmtLbl);
        itemLbls.add(weightLbl);
        itemLbls.add(distToNext);
        
        itemLbls.add(repairAmtLbl);
        
        //November 20th - save load addition
        save = new JButton("Save");
        load = new JButton("Load");
        save.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try {
                	JFileChooser jfc = new JFileChooser();
                	jfc.setDialogTitle("Choose Folder");
                	jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                	int returnVal = jfc.showOpenDialog(getParent());
                	if (returnVal == JFileChooser.APPROVE_OPTION) {
                         // File file = jfc.getSelectedFile();
                          File dir = jfc.getSelectedFile();
                          saver.save(dir);
                	  }
                }
                catch(Exception exc){
     			   exc.printStackTrace(); 
     			}
            }
        });
        
        load.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try {
                JFileChooser load = new JFileChooser();
            	load.setSelectedFile(new File("Save.sav"));
            	int returnVal = load.showOpenDialog(getParent());
            	if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File loadFile = load.getSelectedFile();
                    saver.load(loadFile);
                  	updateHUD(1);
                  	status.firePropertyChange("load", false, true);
            	  }
                }
                catch(Exception exc){
     			   exc.printStackTrace(); 
     			}
            }
        });
        
        
        JPanel savePanel = new JPanel(new GridLayout(2,1));
        savePanel.setBackground(new Color(84,23, 0));
        savePanel.add(save);
        savePanel.add(load);
        //controlsPanel.add(savePanel);
        itemLbls.add(savePanel);
        itemLbls.add(totalDistance);
        
        itemLbls.add(medicineAmtLbl);

        JPanel main = new JPanel();
        main.setAlignmentX(CENTER_ALIGNMENT);

        //CONSTRUCT CONTROLS PANEL - pace and ration drop down buttons 
        JPanel controlsPanel = new JPanel();
        GridBagLayout gbl_controlsPanel = new GridBagLayout();
        gbl_controlsPanel.columnWidths = new int[] { 36, 67, 0 };
        gbl_controlsPanel.rowHeights = new int[] { 20, 20, 20 };
        gbl_controlsPanel.columnWeights = new double[] { 1.0, 1.0,
                Double.MIN_VALUE };
        gbl_controlsPanel.rowWeights = new double[] { 1.0, 1.0,
                1.0 };
        controlsPanel.setLayout(gbl_controlsPanel);
        
        //controlsPanel.add(map);

        JLabel paceLbl = new JLabel("Pace", SwingConstants.TRAILING);
        GridBagConstraints gbc_paceLbl = new GridBagConstraints();
        gbc_paceLbl.anchor = GridBagConstraints.SOUTHEAST;
        gbc_paceLbl.insets = new Insets(0, 0, 5, 5);
        gbc_paceLbl.gridx = 0;
        gbc_paceLbl.gridy = 0;
        controlsPanel.add(paceLbl, gbc_paceLbl);

        paceCB = new JComboBox(new String[] { "10 Knots", "20 Knots",
                "40 Knots" });
        paceCB.setSelectedIndex(0);
        paceCB.addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                ship.setPace(Integer.parseInt(e.getItem().toString()
                        .substring(0, 2)));
              //  System.out.println("You have changed the pace to " +Integer.parseInt(e.getItem().toString()
                        //.substring(0, 2)) );
            }
        });

        paceLbl.setLabelFor(paceCB);

        GridBagConstraints gbc_paceCB = new GridBagConstraints();
        gbc_paceCB.anchor = GridBagConstraints.SOUTHWEST;
        gbc_paceCB.insets = new Insets(0, 0, 5, 0);
        gbc_paceCB.gridx = 1;
        gbc_paceCB.gridy = 0;
        controlsPanel.add(paceCB, gbc_paceCB);

        JLabel rationsLbl = new JLabel("Rations", SwingConstants.TRAILING);
        GridBagConstraints gbc_rationsLbl = new GridBagConstraints();
        gbc_rationsLbl.anchor = GridBagConstraints.NORTHEAST;
        gbc_rationsLbl.insets = new Insets(0, 0, 0, 5);
        gbc_rationsLbl.gridx = 0;
        gbc_rationsLbl.gridy = 1;
        controlsPanel.add(rationsLbl, gbc_rationsLbl);

        rationsCB = new JComboBox(new String[] { "Meager",
                "Modest", "Well Fed" });
        rationsCB.setSelectedIndex(0);
        
        rationsCB.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                String ration = e.getItem().toString();

                if (ration == "Well Fed") ship.setRations(3);
                else if (ration == "Modest") ship.setRations(2);
                else if (ration == "Meager") ship.setRations(1);
               // System.out.println("You have changed the rations to " + ration);
            }
        });

        rationsLbl.setLabelFor(rationsCB);

        GridBagConstraints gbc_rationsCB = new GridBagConstraints();
        gbc_rationsCB.anchor = GridBagConstraints.NORTHWEST;
        gbc_rationsCB.gridx = 1;
        gbc_rationsCB.gridy = 1;
        controlsPanel.add(rationsCB, gbc_rationsCB);
        //END CONTROLS PANEL CONSTRUCTION

        add(itemLbls);
        add(controlsPanel);
        add(map);
        
       /* //November 20th - save load addition
        save = new JButton("Save");
        load = new JButton("Load");
        save.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try {
                	JFileChooser jfc = new JFileChooser();
                	jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                	int returnVal = jfc.showOpenDialog(getParent());
                	if (returnVal == JFileChooser.APPROVE_OPTION) {
                         // File file = jfc.getSelectedFile();
                          File dir = jfc.getSelectedFile();
                          saver.save(dir);
                	  }
                }
                catch(Exception exc){
     			   exc.printStackTrace(); 
     			}
            }
        });
        
        load.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try {
                JFileChooser load = new JFileChooser();
            	load.setSelectedFile(new File("Save.sav"));
            	int returnVal = load.showOpenDialog(getParent());
            	if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File loadFile = load.getSelectedFile();
                    saver.load(loadFile);
                  	updateHUD(1);
                  	status.firePropertyChange("load", false, true);
            	  }
                }
                catch(Exception exc){
     			   exc.printStackTrace(); 
     			}
            }
        });*/
        //add space
     /*   JPanel savePanel = new JPanel(new GridLayout(2,1));
        savePanel.setBackground(new Color(84,23, 0));
        savePanel.add(save);
        savePanel.add(load);
        controlsPanel.add(savePanel);*/
        //controlsPanel.add(save);
        
        //end save load addition
        
    }
    
    /**
     * Adds story as a listener to this class, mainly so that the HUD can communicate to story that a game has been loaded and 
     * to restart at the last state.
     * @param story the Story object for this game
     */
    public void addStoryListener(Story story){
    	status.addPropertyChangeListener(story);
    }

    /**
     * Updates the changed components
     * @param changed If the HUD is to be changed or not
     */
    public void updateHUD(int changed)
    {
        foodAmtLbl.setText("Current food: " + ship.inventory.getFood());
        waterAmtLbl.setText("Current water: " + ship.inventory.getWater());
        repairAmtLbl.setText("Current repair kits: "+ ship.inventory.getRepair());
        medicineAmtLbl.setText("Current medicine: "+ ship.inventory.getMedicine());
        moneyLbl.setText("Current money: " + ship.inventory.getMoney());
        weightLbl.setText("Current weight: " + ship.inventory.getWeight()+"/"+ ship.getInventory().getMaxWeight());
        totalDistance.setText("Total Distance: " + timeline.getCurrentDist() +"/" +timeline.getTotalDist());
        distToNext.setText("Distance To Next Stop: " + timeline.getDistToNext());
        stopName.setText("Current stop: "+timeline.currentStopName());
        // add map update
        if (changed == 1)
        	mapClass.movePosition();
        mapClass.repaint();
        
        //rationsCB.setSelectedIndex(ship.getRations()-1);
        //paceCB.setSelectedIndex((ship.getPace()/10)-1);
    }
}